package com.dianrui.operation.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.dianrui.operation.R;
import com.dianrui.operation.activity.ChangeBatteryActivity;
import com.dianrui.operation.activity.ChoosePointActivity;
import com.dianrui.operation.activity.DeviceBindActivity;
import com.dianrui.operation.activity.DeviceBoxStateInputActivity;
import com.dianrui.operation.activity.DeviceDefenseActivity;
import com.dianrui.operation.activity.DeviceDetectionActivity;
import com.dianrui.operation.activity.TbitbleCommandActivity;
import com.dianrui.operation.activity.DeviceOnlineActivity;
import com.dianrui.operation.activity.VehicleInspectionActivity;
import com.dianrui.operation.activity.VehicleSchedulingMainActivity;
import com.dianrui.operation.activity.RepairOrderActivity;
import com.dianrui.operation.adapter.MainMenuAdapter;
import com.dianrui.operation.bean.Menu;
import com.dianrui.operation.util.MyUtil;
import com.dianrui.operation.util.StringUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class OpetationFragment extends BaseFragment {
    private List<Menu> menuList=new ArrayList<>();
    private MainMenuAdapter menuAdapter;

    @BindView(R.id.main_gridview)
    GridView mainGridview;



    @Override
    public int getContentViewId() {
        return R.layout.fragment_opertion;
    }

    @Override
    public void initView() {
        menuAdapter=new MainMenuAdapter(getActivity(),menuList);
        mainGridview.setAdapter(menuAdapter);
        mainGridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
        getMenu();
        mainGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Menu menu = menuList.get(position);
                switch (menu.menu_id){
                    case "212"://盒子检测：212（A）
                        if (!MyUtil.isGpsEnable(getActivity())) {
                            showOpenGpsDialog();
                            return;
                        }
                        Intent intent=new Intent(getActivity(),DeviceDetectionActivity.class);
                        Bundle bundle=new Bundle();
                        bundle.putString("title",getString(R.string.main_box_statusa));
                        intent.putExtras(bundle);
                        startActivity(intent);
                        break;
                    case "213"://盒子绑定：213
                        Intent intent1=new Intent(getActivity(),DeviceBindActivity.class);
                        Bundle bundle1=new Bundle();
                        bundle1.putString("title",menu.menu_name);
                        intent1.putExtras(bundle1);
                        startActivity(intent1);
                        break;

                    case "214"://盒子状态：214
                        Intent intent2=new Intent(getActivity(),DeviceBoxStateInputActivity.class);
                        Bundle bundle2=new Bundle();
                        bundle2.putString("title",menu.menu_name);
                        intent2.putExtras(bundle2);
                        startActivity(intent2);
                        break;

                    case "215"://上，下线：215
                        Intent intent3=new Intent(getActivity(),DeviceOnlineActivity.class);
                        Bundle bundle3=new Bundle();
                        bundle3.putString("title",menu.menu_name);
                        intent3.putExtras(bundle3);
                        startActivity(intent3);
                        break;

                    case "216"://换电池：216
                        if (!MyUtil.isGpsEnable(getActivity())) {
                            showOpenGpsDialog();
                            return;
                        }
                        JumpActivitys(ChangeBatteryActivity.class);
                        break;

                    case "217"://停车区：217
                        if (!MyUtil.isGpsEnable(getActivity())) {
                            showOpenGpsDialog();
                            return;
                        }
                        Intent intent4=new Intent(getActivity(),ChoosePointActivity.class);
                        Bundle bundle4=new Bundle();
                        bundle4.putString("title",menu.menu_name);
                        intent4.putExtras(bundle4);
                        startActivity(intent4);
                        break;

                    case "218"://维修工单：218
                        Intent intent5=new Intent(getActivity(),RepairOrderActivity.class);
                        Bundle bundle5=new Bundle();
                        bundle5.putString("title",menu.menu_name);
                        intent5.putExtras(bundle5);
                        startActivity(intent5);
                        break;

                    case "219"://巡检：219

                        if (!MyUtil.isGpsEnable(getActivity())) {
                            showOpenGpsDialog();

                        } else {
                            Intent intent7=new Intent(getActivity(),VehicleInspectionActivity.class);
                            Bundle bundle7=new Bundle();
                            bundle7.putString("title",menu.menu_name);
                            intent7.putExtras(bundle7);
                            startActivity(intent7);

                        }

                        break;

                    case "220"://巡检：调度
                        if (!MyUtil.isGpsEnable(getActivity())) {
                            showOpenGpsDialog();
                            return;
                        }
                        Intent intent6=new Intent(getActivity(),VehicleSchedulingMainActivity.class);
                        Bundle bundle6=new Bundle();
                        bundle6.putString("title",menu.menu_name);
                        intent6.putExtras(bundle6);
                        startActivity(intent6);
                        break;

                    case "221"://设防/撤防
                        Intent intent8=new Intent(getActivity(),DeviceDefenseActivity.class);
                        Bundle bundle8=new Bundle();
                        bundle8.putString("title",menu.menu_name);
                        intent8.putExtras(bundle8);
                        startActivity(intent8);
                        break;

                    case "222"://网络盒子（T）
                        Intent intent9=new Intent(getActivity(),TbitbleCommandActivity.class);
                        Bundle bundle9=new Bundle();
                        bundle9.putString("title",getString(R.string.main_box_statust));
                        intent9.putExtras(bundle9);
                        startActivity(intent9);
                        break;

                        default:
                            break;
                }
            }
        });
    }

    private void getMenu() {
        if (!StringUtils.isEmpty(spUtils.getString("menu"))) {
            String json =spUtils.getString("menu");
            try {
                final JSONObject jsonObject = new JSONObject(json);
                JSONArray jsonArray3 = jsonObject.optJSONObject("data").optJSONArray("menu_list");
                if (jsonArray3.length() > 0) {
                    for (int i = 0; i < jsonArray3.length(); i++) {
                        Menu menu = new Menu();
                        menu.menu_id = jsonArray3.optJSONObject(i).optString("menu_id");
                        menu.menu_name = jsonArray3.optJSONObject(i).optString("menu_name");
                        menu.menu_action = jsonArray3.optJSONObject(i).optString("menu_action");
                        menuList.add(menu);
                    }
                    if (menuAdapter != null) {
                        menuAdapter.refresh(menuList);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    private void showOpenGpsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.activity_dialog);
        final View view2 = LayoutInflater.from(getActivity()).inflate(R.layout.open_gps_dialog, null);
        final AlertDialog pausedialog = builder.create();
        pausedialog.setView(view2);
        pausedialog.setCanceledOnTouchOutside(false);
        pausedialog.show();
        final Button yes = view2.findViewById(R.id.yes);

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pausedialog.dismiss();
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
    }
}
